package com.talent.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.talent.page.Page;
import com.talent.pojo.Course;
import com.talent.service.CourseService;
import com.talent.vo.Result;


/**
 * 课程
 */
@Controller
@RequestMapping("/manage/course")
public class CourseController extends BaseController{

    @Autowired
    CourseService courseService;


    @RequestMapping(value = "/manager",method = RequestMethod.GET)
    public ModelAndView courseUI(Course course, Integer currentPage, ModelAndView mav){
        try {
            Page<Course> page = courseService.selectCoursePage(course, currentPage == null ? 1 : currentPage);
            // 列表项
            mav.addObject("page", page.getResultList());
            // 分页参数
            initPageParams(mav, page);
            // 搜索条件
            mav.addObject("course", course);
            mav.addObject("courseList",page.getResultList());
            mav.setViewName("course/manager");
        }catch (Exception e){
            mav.setViewName("error/500");
            e.printStackTrace();
        }
        return mav;
    }


    /**
     * @Title: addUI
     * @Description: 去添加页面
     * @return
     */

    @RequestMapping("addUI")
	public ModelAndView courseAddUI() {
		ModelAndView mav = new ModelAndView();
		try {
			mav.setViewName("/course/addUI");
		} catch (Exception e) {
			e.printStackTrace();
			mav.setViewName("common/error");
			mav.addObject("error", "有异常！");
		}
		return mav;
	}

    @RequestMapping("add")
	@ResponseBody
	public Result courseAdd(Course course) {
		try {
			courseService.addCourse(course);
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
	}
    @RequestMapping(value = "codeCheck", method = RequestMethod.POST)
    @ResponseBody
	public Object codeCheck(String code) {
		try {
			int count = courseService.selectCount(code.trim());
				if(count>0){
					return renderError("已存在");
				}
				return renderSuccess("");
		} catch (Exception e) {
			return renderError("班级编号出错");
		}

	} 
    @RequestMapping("courseDetail")
	public ModelAndView courseDetail(Integer id) {
		ModelAndView mav = new ModelAndView();
		try {
			Course course = courseService.selectById(id);
			mav.addObject("course", course);
			mav.setViewName("course/courseDetail");
		} catch (Exception e) {
			e.printStackTrace();
			mav.setViewName("common/error");
			mav.addObject("error", "有异常！");
		}
		return mav;
	}

    /**
     * 批量删除
     * @return
     */
    @RequestMapping("courseDeleteIDs")
    @ResponseBody
    public Result deleteIDs(String ids) {
    	try {
    		courseService.deleteAll(ids.split(","));
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("courseDelete")
	@ResponseBody
	public Result classDelete(Integer id) {
		try {
			courseService.deleteById(id);
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
	}
    /**
     * 编辑
     * @param id
     * @return
     */
    @RequestMapping("courseUpdateUI")
	public ModelAndView courseUpdateUI(Integer id) {
		ModelAndView mav = new ModelAndView();
		try {
			Course course = courseService.selectById(id);
			mav.addObject("course", course);
			mav.setViewName("course/courseUpdate");
		} catch (Exception e) {
			e.printStackTrace();
			mav.setViewName("common/error");
			mav.addObject("error", "有异常！");
		}
		return mav;
	}

	@RequestMapping("courseUpdate")
	@ResponseBody
	public Result classUpdate(Course course) {

		try {
			courseService.editCourse(course);
			return new Result(true);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
	}
}
